﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraEditors.Popup;
using DXApplicationTangche.UC.门店下单;
using DevExpress.XtraGrid.Editors;
using DevExpress.Utils.Win;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.套号管理.电商下单;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.销售记录.service;
using static DXApplicationTangche.UC.门店下单.form.Frm订单一览;
using DXApplicationTangche.UC.样品管理.样品下单新.service;
using DXApplicationTangche.UC.样品管理.样品下单;

namespace DXApplicationTangche.UC.套号管理.经销商下单
{
    public partial class Form大货下单 : DevExpress.XtraEditors.XtraForm
    {
        //model
        private OrderModel orderModel = new OrderModel();
        //变量
        private CustomerChooseInfoDTO customerChooseInfoDTO = new CustomerChooseInfoDTO();//大货客户DTO
        private String salesType = "受托加工";
        private String salesChannel;
        public Form大货下单()
        {
            InitializeComponent();
            this.searchLookUpEdit选择客户.Properties.DataSource = 样品下单Service.getOutSourcingCustomer();
        }
        private void refreshGrid()
        {
            this.gridControl经销商订单.DataSource = this.orderModel.orderCommitDTO返回s;
            this.gridView经销商订单.RefreshData();
        }
        #region 选择用户
        private void searchLookUpEdit选择客户_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }

        private void searchLookUpEdit选择客户View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit选择客户.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                this.customerChooseInfoDTO.customerId //  no
                    = this.searchLookUpEdit选择客户.Properties.View.GetRowCellValue(rowHandle, "customerId").ToString();//id 是 Value Member
                this.customerChooseInfoDTO.customerName //  no
                    = this.searchLookUpEdit选择客户.Properties.View.GetRowCellValue(rowHandle, "customerName").ToString();//id 是 Value Member
                this.customerChooseInfoDTO.mobile
                    = this.searchLookUpEdit选择客户.Properties.View.GetRowCellValue(rowHandle, "mobile").ToString();//id 是 Value Member
                try
                {
                    this.customerChooseInfoDTO.addressId //  no
                    = this.searchLookUpEdit选择客户.Properties.View.GetRowCellValue(rowHandle, "addressId").ToString();//id 是 Value Member
                }
                catch { }
                this.salesChannel = this.searchLookUpEdit选择客户.Properties.View.GetRowCellValue(rowHandle, "customerName").ToString();
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit选择客户.ToolTip = null;
            searchLookUpEdit选择客户.EditValue = null;
        }
        private void searchLookUpEdit选择客户_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.customerChooseInfoDTO.customerName;
        }
        #endregion

        private void simpleButton添加订单_Click(object sender, EventArgs e)
        {
            new Form经销商选择下单(this.orderModel).ShowDialog();
            this.refreshGrid();
        }

        private void simpleButton确认下单_Click(object sender, EventArgs e)
        {
            if (this.customerChooseInfoDTO.customerId == null)
            {
                MessageBox.Show("经销商为空,请选择经销商");
                return;
            }
            if (this.orderModel.orderCommitDTO返回s.Count <= 0)
            {
                MessageBox.Show("无下单内容");
                return;
            }
            foreach (OrderCommitDTO返回 orderCommitDto in this.orderModel.orderCommitDTO返回s)
            {
                //给订单客户属性赋值
                orderCommitDto.build给销售渠道赋值(this.salesType, this.salesChannel);
            }
            销售记录Service.post销售记录(
                new DTO销售记录().build套号(orderModel.orderCommitDTO返回s),
                Enum订单类别.套号订单
                );
            this.Close();
        }

        private void simpleButton新增客户_Click(object sender, EventArgs e)
        {
            new Form创建委外客户().ShowDialog();
            this.searchLookUpEdit选择客户.Properties.DataSource = 样品下单Service.getOutSourcingCustomer();
            this.searchLookUpEdit选择客户.Refresh();
        }
    }
}